<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Login as LoginModel;
/**
*@author  于江<18547145944@163.com>
*登录
*/
class Login extends Controller
{	
	
	// 模板显示
	public function index(){

		if (request()->isPost()) {
			$admin=new LoginModel();
			$data=input('post.');
			$num=$admin->login($data);
			if ($num==3) {
				echo "3";//登录成功！
			}elseif ($num==4) {
				echo "4";//验证码不正确
			}else{
				echo "2";//用户名或密码错误
				
			}
			return;
		}
		$this->getIP();//获取用户登录ip
		return view();
	}
	 // 获得用户登录ip地址
	public function getIP(){
		global $ip;
	    if (getenv("HTTP_CLIENT_IP"))
	        $ip = getenv("HTTP_CLIENT_IP");
	    else if(getenv("HTTP_X_FORWARDED_FOR"))
	        $ip = getenv("HTTP_X_FORWARDED_FOR");
	    else if(getenv("REMOTE_ADDR"))
	        $ip = getenv("REMOTE_ADDR");
	    else $ip = "Unknow IP";
	    // return $ip;
	    session('ip',$ip);//把ip存入session
	}
	// 发送短信验证码 
	public function message(){
		include_once('sms.php');
		$code=$this->createSMSCode();//随机数
		$target = "http://cf.51welink.com/submitdata/Service.asmx/g_Submit";
		//替换成自己的测试账号,参数顺序和wenservice对应
		$post_data = "sname=dlxincs&spwd=dlxincs521128&scorpid=&sprdid=1012818&sdst=15124737674&smsg=".rawurlencode("您的验证码是：'$code'。请不要把验证码泄露给其他人。【微网通联】");
		//$binarydata = pack("A", $post_data);
		echo $gets = Post($post_data, $target);
		//请自己解析$gets字符串并实现自己的逻辑
		//<State>0</State>表示成功,其它的参考文档

	}
	// 生成短信验证码
	public function createSMSCode($length = 4){
			$min = pow(10 , ($length - 1));
			$max = pow(10, $length) - 1;
			return rand($min, $max);
	}
		// 验证短信验证码是否有效
		public function checkSMSCode(){
		    $mobile = $_POST['mobile'];
		    $code = $_POST['code'];
		    $nowTimeStr = date('Y-m-d H:i:s');
		    $smscodeObj =db('code')->where("mobile='$mobile'")->find();
		    if($smscodeObj){
		        $smsCodeTimeStr = $smscodeObj['update_at'];
		        $recordCode = $smscodeObj['code'];
		        $flag = $this->checkTime($nowTimeStr, $smsCodeTimeStr);
		        if(!$flag){
		            $result = array(
		                'code' => '1',
		                'ext' => '验证码过期，请刷新后重新获取'
		            );
		            echo json_encode($result,JSON_UNESCAPED_UNICODE);
		            return;
		        }

		        if($code != $recordCode){
		            $result = array(
		                'code' => '2',
		                'ext' => '验证码错误，请重新输入'
		            );
		            echo json_encode($result,JSON_UNESCAPED_UNICODE);
		            return;
		        }

		        $result = array(
		            'code' => '0',
		            'ext' => '验证通过'
		        );
		        echo json_encode($result,JSON_UNESCAPED_UNICODE);
		    }
		}

		// 验证验证码时间是否过期
		public function checkTime($nowTimeStr,$smsCodeTimeStr){
		    //$nowTimeStr = '2016-10-15 14:39:59';
		    //$smsCodeTimeStr = '2016-10-15 14:30:00';
		    $nowTime = strtotime($nowTimeStr);
		    $smsCodeTime = strtotime($smsCodeTimeStr);
		    $period = floor(($nowTime-$smsCodeTime)/60); //60s
		    if($period>=0 && $period<=20){
		        return true;
		    }else{
		        return false;
		    }
		}


}